package com.ldgame.game;

import org.lwjgl.opengl.Display;
import org.lwjgl.util.vector.Vector2f;
import org.newdawn.slick.Color;

import com.ldgame.gfx.Screen;
import com.ldgame.gfx.gfxelements.Label;
import com.ldgame.input.Input;
import com.ldgame.sound.Sound;
import com.ldgame.sound.SoundSystem;
import com.ldgame.utils.Timer;

public class Game implements Runnable {
	private Screen screen = new Screen(800, 600);
	private GameState gameState;
	private GameState menu = new MenuState(this);
	private RunningState runningGame = null;
	private GameState howTo = new HowToState(this);
	private GameState victory = new VictoryState(this);
	private GameState death = new DeathState(this);
	private GameState defeat = new DefeatState(this);
	
	private boolean running = true;
	private Thread gameThread;
	
	private Label unfocusedLabel = new Label(24.0f, new Color(1.0f, 0.5f, 0.0f, 1.0f));
	//private Label fpsLabel = new Label(8.0f, new Color(1.0f, 1.0f, 1.0f, 1.0f));
	
	private Vector2f unfocusedLabelPos = new Vector2f(400.0f, 300.0f);
	//private Vector2f fpsLabelPos = new Vector2f(775.0f, 15.0f);
	
	public void start() {
		gameThread = new Thread(this);
		gameThread.start();
	}
	
	@Override
	public void run() {
		setGameState(menu);
		
		screen.init();
		SoundSystem.init();
		SoundSystem.playSound(Sound.THEME);
		while(running) {
			if (Display.isActive()) {
				Timer.syncToSystemTime();
				
				Input.listen();
				gameState.handleInput();

				gameState.calcConfig();
				
				screen.renderStart();
				gameState.render();
				
				//fpsLabel.render(Integer.toString(Timer.currentFPS), fpsLabelPos);
				Timer.calcLoopTime();
				Timer.calcFPS();
			} else {
				screen.renderStart();
				unfocusedLabel.render("CLICK TO FOCUS", unfocusedLabelPos);
			}
		}
		
		Display.destroy();
		SoundSystem.destroy();
	}

	public boolean isRunning() {
		return running;
	}

	public void setRunning(boolean running) {
		this.running = running;
	}
	
	public Thread getGameThread() {
		return gameThread;
	}
	
	public GameState getGameState() {
		return gameState;
	}

	public void setGameState(GameState gameState) {
		this.gameState = gameState;
	}

	public GameState getMenu() {
		return menu;
	}

	public RunningState getRunningGame() {
		return runningGame;
	}

	public void setRunningGame(RunningState runningGame) {
		this.runningGame = runningGame;
	}
	
	public GameState getHowTo() {
		return howTo;
	}

	public void setHowTo(GameState howTo) {
		this.howTo = howTo;
	}

	public GameState getVictory() {
		return victory;
	}

	public void setVictory(GameState victory) {
		this.victory = victory;
	}
	
	public GameState getDeath() {
		return death;
	}

	public void setDeath(GameState death) {
		this.death = death;
	}

	public GameState getDefeat() {
		return defeat;
	}

	public void setDefeat(GameState defeat) {
		this.defeat = defeat;
	}
}
